//    Copyright (C) Kherty.  All rights reserved.
using System.Collections.Generic;
using OpenLS.Core.Serialization;

namespace OpenLS.Drawing.Diagram
{
    internal class When : IOfficeXmlSerializable
    {
        private VariableType _arg;
        private FunctionType _func;
        private IteratorAttributes _IteratorAttributes = new IteratorAttributes();
        private string _name;
        private FunctionOperator _op;
        private string _value;
        private readonly List<IOfficeXmlSerializable> _items = new List<IOfficeXmlSerializable>()
            ;

        #region IOfficeXmlSerializable Members

        public void ReadXml(ReadContext context)
        {
            using (ReadContext c = context.Read("if"))
            {
                _name = c.GetOptionalString("name", string.Empty);
                _IteratorAttributes.ReadXml(c);
                _func = c.GetEnum<FunctionType>("func");
                _arg = c.GetOptionalEnum<VariableType>("arg", VariableType.None);
                _op = c.GetEnum<FunctionOperator>("op");
                _value = c.GetString("val");
                _items.Clear();
                _items.AddRange(ReaderHelper.ReadItems(c));

            }
        }

        public void WriteXml(WriteContext context)
        {
            using (WriteContext c = context.Write("if"))
            {
                c.SetOptionalString("name", _name, string.Empty);
                _IteratorAttributes.WriteXml(c);
                c.SetEnum<FunctionType>("func", _func);
                c.SetOptionalEnum<VariableType>("arg", _arg, VariableType.None);
                c.SetEnum<FunctionOperator>("op", _op);
                c.SetString("val", _value);
                c.WriteElements(_items);               
            }
        }

        #endregion
    }
}